﻿using System;
using System.ComponentModel;
using System.Data;
using ReflectionStudio.Core.Events;
using System.Windows;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for EditEventWindow.xaml
    /// </summary>
    public partial class Y3TGAAUF : UIGAAUF
    {
        #region ==dwReq===========
        public static readonly DependencyProperty dwReqProperty = DependencyProperty.Register("dwReq",
        typeof(DataRow), typeof(Y3TGAAUF),
        new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OdwReqChanged));

        public DataRow dwReq
        {
            get
            {
                return (DataRow)GetValue(dwReqProperty);
            }
            set
            {
                SetValue(dwReqProperty, value);
                CHKYM = true;
                CHKSM = true;
                LOADBYREQ(dwReq, true, true);
            }
        }
        private static void OdwReqChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as Y3TGAAUF;
            if (src != null)
            {
                DataRow row = (DataRow)e.NewValue;
                src.dwReq = row;
            }
        }
        #endregion

        #region ==EXTMU===========
        public static readonly DependencyProperty EXTMUProperty = DependencyProperty.Register("EXTMU",
            typeof(string), typeof(Y3TGAAUF),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnEXTMUChanged));
        public string EXTMU
        {
            get
            {
                return (string)GetValue(EXTMUProperty);
            }
            set
            {
                SetValue(EXTMUProperty, value);
            }
        }
        private static void OnEXTMUChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as Y3TGAAUF;
            if (src != null)
            {
                src.EXTMU = (string)e.NewValue;
            }
        }
        #endregion

        #region ==CHKYM===========
        public static readonly DependencyProperty CHKYMProperty = DependencyProperty.Register("CHKYM",
            typeof(bool), typeof(Y3TGAAUF),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCHKYMChanged));
        public bool CHKYM
        {
            get
            {
                return (bool)GetValue(CHKYMProperty);
            }
            set
            {
                SetValue(CHKYMProperty, value);
            }
        }
        private static void OnCHKYMChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as Y3TGAAUF;
            if (src != null)
            {
                src.CHKYM = (bool)e.NewValue;
            }
        }
        #endregion

        #region ==CHKSM===========
        public static readonly DependencyProperty CHKSMProperty = DependencyProperty.Register("CHKSM",
            typeof(bool), typeof(Y3TGAAUF),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCHKSMChanged));
        public bool CHKSM
        {
            get
            {
                return (bool)GetValue(CHKSMProperty);
            }
            set
            {
                SetValue(CHKSMProperty, value);
            }
        }
        private static void OnCHKSMChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as Y3TGAAUF;
            if (src != null)
            {
                src.CHKSM = (bool)e.NewValue;
            }
        }
        #endregion


        public Y3TGAAUF()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VGAAUF();
            this.DataContext = MV;
            InitializeComponent();
        }
        public void LOADBYREQ(DataRow dwGA, bool IsMove = true, bool IsClear = true)
        {
            if (IsClear)
            {

                TBMain.Clear();
            }
            if (dwReq == null)
                return;
            dwData = TBMain.NewRow();
            dwData["GAMANDANT"] = dwGA["GAMANDANT"];
            dwData["GAART"] = dwGA["GAART"];
            dwData["GAVAR"] = dwGA["GAVAR"];
            dwData["GAFARBE"] = dwGA["GAFARBE"];
            dwData["GAAUFMSL"] = dwGA["GAAUFMSL"];
            dwData["GAAENDBEN"] = POSTYP;
            SResult = TMV.QGD23(dwData);
            MergeData(SResult, IsMove, IsClear);
            if (TBMain.Rows.Count > 0)
                return;

            dwData = TBMain.Rows.Add();
            dwData["GAMANDANT"] = dwGA["GAMANDANT"];
            dwData["GAART"] = dwGA["GAART"];
            dwData["GAVAR"] = dwGA["GAVAR"];
            dwData["GAFARBE"] = dwGA["GAFARBE"];
            dwData["GAAUFMSL"] = dwGA["GAAUFMSL"];
            dwData["GAKG1"] = 0;
            dwData["GAKG2"] = 0;
            dwData["GAKG3"] = 0;
            dwData["GAKG5"] = 0;
            dwData["GAKG0"] = 0;
            vwMaster.View.MoveCurrentToFirst();

        }
        public void CHEKYUMAN()
        {
            if (dwGD == null)
                return;
            if (!CHKYM)
                return;
            CHKYM = false;
            DataRow dwTemp = TBMain.Clone().Rows.Add();
            dwTemp.ItemArray = dwGD.ItemArray;
            DataRow dwGS = VGASTGA.Instance.TBMain.NewRow();
            dwGS["GAGMANDANT"] = app.Mandant;
            dwGS["GAGART"] = dwGD["GAART"];
            dwGS["GAGVAR"] = dwGD["GAVAR"];
            DataSet Set = VGASTGA.Instance.QueryGASTGA(dwGS, 9);
            foreach (DataRow row in Set.Tables[0].Rows)
            {
                dwTemp["GAART"] = row["GAGGART"];
                dwTemp["GAVAR"] = row["GAGGVAR"];
                dwTemp["GAFARBE"] = row["GAGGFARBE"];
                dwTemp["GAAUFMSL"] = EXTMU;
                LOADBYREQ(dwTemp, false, false);
                break;
            }

        }
        public void LOADSM()
        {
            if (dwGD == null)
                return;
            if (!CHKSM)
                return;
            CHKSM = false;
            if (EXTMU.Trim().Length == 0)
                return;
            if (EXTMU == dwGD["GAAUFMSL"].ToString())
                return;
            DataRow dwTemp = TBMain.Clone().Rows.Add();
            dwTemp.ItemArray = dwGD.ItemArray;
            //散棉的可用性 
            dwTemp["GAAUFMSL"] = EXTMU;
            LOADBYREQ(dwTemp, false, false);
            //原棉的可用性 
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);

            try
            {
                LOADSM();
                CHEKYUMAN();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}
